package ui;

import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.ParallelTransition;
import javafx.animation.Timeline;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.BlurType;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Glow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import javafx.util.Duration;

public class ComingSoonPage {
    public void show(Stage stage) {
        StackPane root = new StackPane();
        root.setAlignment(Pos.CENTER);

        // 背景图片
        Image bgImage;
        try {
            bgImage = new Image(getClass().getResourceAsStream("/images/AI2.jpg"));
            ImageView bgView = new ImageView(bgImage);
            bgView.setFitWidth(1400);
            bgView.setFitHeight(900);
            bgView.setPreserveRatio(true);
            root.getChildren().add(bgView);
            
            // 添加模糊效果
            bgView.setEffect(new javafx.scene.effect.GaussianBlur(10));
        } catch (Exception e) {
            Rectangle fallback = new Rectangle(1300, 900, Color.web("#2D3447"));
            root.getChildren().add(fallback);
        }

        // 半透明渐变遮罩
        Rectangle overlay = new Rectangle(1300, 900);
        overlay.setFill(Color.rgb(13, 20, 41, 0.7));
        root.getChildren().add(overlay);
        
        // 创建装饰性圆形元素
        createDecorationCircles(root);

        VBox vbox = new VBox(50);
        vbox.setAlignment(Pos.CENTER);

        // 标题文本
        Text title = new Text("功能开发中");
        title.setStyle("-fx-font-size: 60px; -fx-font-family: 'Microsoft YaHei UI Light'; -fx-fill: linear-gradient(to bottom, #FFFFFF 20%, #7EB6FF 90%); -fx-effect: dropshadow(gaussian, #00A6FF, 20, 0, 0, 0);");
        
        // 副标题
        Text subtitle = new Text("敬请期待");
        subtitle.setStyle("-fx-font-size: 48px; -fx-font-family: 'Microsoft YaHei UI Light'; -fx-fill: linear-gradient(to bottom, #FFFFFF 20%, #7EB6FF 90%); -fx-effect: dropshadow(gaussian, #00A6FF, 15, 0, 0, 0);");
        subtitle.setTextAlignment(TextAlignment.CENTER);
        
        // 描述文本
        Text description = new Text("我们正在努力为您开发更多精彩功能\n感谢您的耐心等待");
        description.setStyle("-fx-font-size: 24px; -fx-font-family: 'Microsoft YaHei UI Light'; -fx-fill: #FFFFFF; -fx-opacity: 0.8;");
        description.setTextAlignment(TextAlignment.CENTER);

        // 创建按钮容器
        HBox buttonBox = new HBox(20);
        buttonBox.setAlignment(Pos.CENTER);

        // 返回按钮
        Button backBtn = new Button("返回主菜单");
        styleButton(backBtn);
        backBtn.setOnAction(e -> {
            MenuPage menuPage = new MenuPage();
            try {
                menuPage.start(new Stage());
                stage.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });

        buttonBox.getChildren().add(backBtn);
        vbox.getChildren().addAll(title, subtitle, description, buttonBox);
        root.getChildren().add(vbox);

        // 添加动画效果
        addAnimations(title, subtitle, description, buttonBox);

        Scene scene = new Scene(root, 1300, 900);
        scene.getStylesheets().add("/css/menu.css");
        stage.setScene(scene);
        stage.setTitle("敬请期待");
        stage.show();
    }
    
    private void styleButton(Button button) {
        button.setStyle(
            "-fx-font-size: 20px; " +
            "-fx-background-radius: 30; " +
            "-fx-padding: 15 40; " +
            "-fx-background-color: linear-gradient(to right, #0072ff, #00c6ff); " +
            "-fx-text-fill: white; " +
            "-fx-font-weight: bold; " +
            "-fx-effect: dropshadow(three-pass-box, rgba(0,114,255,0.5), 10, 0, 0, 5);"
        );
        
        // 添加鼠标悬停效果
        button.setOnMouseEntered(e -> 
            button.setStyle(
                "-fx-font-size: 20px; " +
                "-fx-background-radius: 30; " +
                "-fx-padding: 15 40; " +
                "-fx-background-color: linear-gradient(to right, #00c6ff, #0072ff); " +
                "-fx-text-fill: white; " +
                "-fx-font-weight: bold; " +
                "-fx-effect: dropshadow(three-pass-box, rgba(0,114,255,0.8), 15, 0, 0, 7);"
            )
        );
        
        button.setOnMouseExited(e -> 
            button.setStyle(
                "-fx-font-size: 20px; " +
                "-fx-background-radius: 30; " +
                "-fx-padding: 15 40; " +
                "-fx-background-color: linear-gradient(to right, #0072ff, #00c6ff); " +
                "-fx-text-fill: white; " +
                "-fx-font-weight: bold; " +
                "-fx-effect: dropshadow(three-pass-box, rgba(0,114,255,0.5), 10, 0, 0, 5);"
            )
        );
    }
    
    private void createDecorationCircles(StackPane root) {
        // 创建几个装饰性的圆形，添加到不同位置
        for (int i = 0; i < 8; i++) {
            double size = Math.random() * 100 + 50; // 50-150的随机大小
            Circle circle = new Circle(size);
            
            // 设置渐变填充和半透明效果
            circle.setFill(Color.rgb(0, 166, 255, 0.1 + Math.random() * 0.1));
            circle.setStroke(Color.rgb(255, 255, 255, 0.2));
            circle.setStrokeWidth(2);
            
            // 随机位置
            double x = Math.random() * 1200 - 600; // -600到600的范围
            double y = Math.random() * 800 - 400;  // -400到400的范围
            
            circle.setTranslateX(x);
            circle.setTranslateY(y);
            
            // 添加到根容器
            root.getChildren().add(circle);
            
            // 为每个圆形添加浮动动画
            createFloatingAnimation(circle);
        }
    }
    
    private void createFloatingAnimation(Circle circle) {
        // 创建浮动效果的动画
        double originalY = circle.getTranslateY();
        double originalX = circle.getTranslateX();
        
        // 创建Y轴动画
        Timeline timelineY = new Timeline(
            new KeyFrame(Duration.ZERO, new KeyValue(circle.translateYProperty(), originalY)),
            new KeyFrame(Duration.seconds(3 + Math.random() * 4), // 3-7秒的随机持续时间
                         new KeyValue(circle.translateYProperty(), originalY + (Math.random() * 60 - 30))) // 上下浮动30像素
        );
        timelineY.setAutoReverse(true);
        timelineY.setCycleCount(Timeline.INDEFINITE);
        
        // 创建X轴动画
        Timeline timelineX = new Timeline(
            new KeyFrame(Duration.ZERO, new KeyValue(circle.translateXProperty(), originalX)),
            new KeyFrame(Duration.seconds(4 + Math.random() * 4), // 4-8秒的随机持续时间
                         new KeyValue(circle.translateXProperty(), originalX + (Math.random() * 60 - 30))) // 左右浮动30像素
        );
        timelineX.setAutoReverse(true);
        timelineX.setCycleCount(Timeline.INDEFINITE);
        
        // 同时播放两个动画
        timelineY.play();
        timelineX.play();
    }
    
    private void addAnimations(Text title, Text subtitle, Text description, HBox buttonBox) {
        // 标题淡入动画
        FadeTransition fadeTitle = new FadeTransition(Duration.seconds(1.5), title);
        fadeTitle.setFromValue(0);
        fadeTitle.setToValue(1);
        
        // 副标题淡入动画，稍微延迟
        FadeTransition fadeSubtitle = new FadeTransition(Duration.seconds(1.5), subtitle);
        fadeSubtitle.setFromValue(0);
        fadeSubtitle.setToValue(1);
        fadeSubtitle.setDelay(Duration.seconds(0.5));
        
        // 描述淡入动画，更多延迟
        FadeTransition fadeDesc = new FadeTransition(Duration.seconds(1.5), description);
        fadeDesc.setFromValue(0);
        fadeDesc.setToValue(1);
        fadeDesc.setDelay(Duration.seconds(1));
        
        // 按钮淡入动画，最后出现
        FadeTransition fadeButtons = new FadeTransition(Duration.seconds(1.5), buttonBox);
        fadeButtons.setFromValue(0);
        fadeButtons.setToValue(1);
        fadeButtons.setDelay(Duration.seconds(1.5));
        
        // 同时播放所有动画
        ParallelTransition parallelTransition = new ParallelTransition(
            fadeTitle, fadeSubtitle, fadeDesc, fadeButtons
        );
        parallelTransition.play();
        
        // 为标题添加轻微的脉动效果
        Timeline pulseTimeline = new Timeline(
            new KeyFrame(Duration.ZERO, new KeyValue(title.scaleXProperty(), 1), new KeyValue(title.scaleYProperty(), 1)),
            new KeyFrame(Duration.seconds(2), new KeyValue(title.scaleXProperty(), 1.05), new KeyValue(title.scaleYProperty(), 1.05)),
            new KeyFrame(Duration.seconds(4), new KeyValue(title.scaleXProperty(), 1), new KeyValue(title.scaleYProperty(), 1))
        );
        pulseTimeline.setCycleCount(Timeline.INDEFINITE);
        pulseTimeline.play();
    }
} 